package com.zl.controller;

import com.zl.domain.Project;
import com.zl.domain.Query;
import com.zl.service.ProjectService;
import net.sf.json.JSONObject;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class ProjectController {
    @Autowired
    private ProjectService projectService;

    @RequestMapping(value = "/project/findAllTypes", method = RequestMethod.GET)
    public
    @ResponseBody
    JSONObject findTypes() {
        return projectService.findTypes();
    }

    @RequestMapping(value = "/project/projectList", method = RequestMethod.GET)
    public
    @ResponseBody
    JSONObject projectList(@Param("Query") Query query) {
        return new JSONObject().accumulate("page", projectService.page(query));
    }

    @RequestMapping(value = "/project/delProject", method = RequestMethod.GET)
    public
    @ResponseBody
    JSONObject delProject(@Param("Project") Project project) {
        return new JSONObject().accumulate("message", projectService.delete(project) == 0 ? "删除失败！！" : "删除成功！");
    }

    @RequestMapping(value = "/project/editProject", method = RequestMethod.POST)
    public
    @ResponseBody
    JSONObject editProject(@Param("Project") Project project) {
        String message = "";
        if (project.getPid() == 0)
            message = projectService.insert(project) == 0 ? "添加失败！！" : "添加成功！";
        else
            message = projectService.update(project) == 0 ? "编辑失败！！" : "编辑成功！";
        return new JSONObject().accumulate("message", message);
    }
}
